package com.bpc.cs.managed.bean;

import java.io.Serializable;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.servlet.http.HttpServletRequest;

import org.primefaces.event.RowEditEvent;

import com.bpc.cs.constant.AttributeName;
import com.bpc.cs.constant.Constant;
import com.bpc.cs.managed.dvo.SchemeDVO;
import com.bpc.cs.service.ISchemeService;
import com.bpc.cs.util.Message;

/**
 * @author   phan_tp
 * @datetime Dec 3, 2012 : 11:07:28 AM
 */

public class SchemeBean extends BaseBean implements Serializable {

	private static final long serialVersionUID = 895924817359844259L;

	ISchemeService schemeService;
	private List<SchemeDVO> listScheme;
	private String schemeName;
	private SchemeDVO selectedScheme;

	/**
	 * SchemeBean contructor call BaseBean contructor
	 */
	public SchemeBean() {
		super();
	}

	/**
	 * Post contruct to init list scheme for datatable after init SchemeBean
	 */
	@PostConstruct
	public void init() {
		// Get all scheme to show
		listScheme = getSchemeService().getAllScheme();
	}

	/**
	 * View Rule of Scheme
	 */
	public void viewRuleOfScheme() {
		// Get RuleBean from Context of JSF
		RuleBean ruleBean = (RuleBean)getBean(Constant.RULE_BEAN, RuleBean.class);

		// Set selected schemeID to ruleBean to prepare getting list rule
		ruleBean.setSchemeID(selectedScheme.getSchemeID());

		// Get list rule of scheme
		ruleBean.preGetListRule();

		// Reset selected rule when init list rule
		ruleBean.setSelectedRule(null);
	}

	/**
	 * Modify name of scheme
	 * @param RowEditEvent event
	 */
	public void onEditSchemeName(RowEditEvent event) {
		// Get object edit from RowEditEvent
		SchemeDVO schemeDVO = (SchemeDVO)event.getObject();

		// Call service to execute modification info
		Message msg = schemeService.modifyScheme(schemeDVO.getSchemeID(), schemeDVO.getSchemeName());

		// Flag variable to decide show message or not
		boolean isShowMessage = false;

		// Check result from service
		if (!msg.isResultBoolean()) {
			// Modified scheme failed
			isShowMessage = true;
		}

		// Set attribute to decide view message or not
		getRequest().setAttribute(msg.getMessageKey(), isShowMessage);
	}

	/**
	 * Remove scheme
	 * @param ActionEvent event
	 */
	public void removeScheme(ActionEvent event) {
		if (schemeService.removeScheme(selectedScheme.getSchemeID())) {
			// Scheme hasn't rule
			// Get data again afer delete one record
			init();
		} else {
			// Scheme has rule, get confirmation from user
			HttpServletRequest request = (HttpServletRequest)FacesContext.getCurrentInstance()
																		 .getExternalContext()
																		 .getRequest();
			// Set value to attribute to decide show message or not
			request.setAttribute(AttributeName.SHOW_MSG_SCHEME_HAS_RULE, true);
		}
	}

	/**
	 * Remove entire scheme which contains rule, factorchoice
	 * @param event
	 */
	public void removeEntireScheme(ActionEvent event) {
		// Call schemeService to execute remove entire scheme, rule and factor choice
		schemeService.removeEntireScheme(selectedScheme.getSchemeID());

		// Get data again afer delete one record
		init();
	}

	/**
	 * Add new Scheme Name
	 */
	public void addScheme() {
		// Call service to execute addition info
		Message msg = schemeService.addScheme(schemeName);

		// Check result from service
		if (msg.isResultBoolean()) {
			// Add scheme successfull
			// Reset schemename variable
			schemeName = null;

			// Get list scheme again
			init();
		}

		// Set attribute to decide view message or not
		getRequest().setAttribute(msg.getMessageKey(), !msg.isResultBoolean());
	}

	/**
	 * Get all Scheme
	 * @return the list SchemeDVO
	 */
	public List<SchemeDVO> getListSchemeDVO() {
		return listScheme;
	}

	/**
	 * Get listScheme
	 * @return SchemeDataModel
	 */
	public List<SchemeDVO> getListScheme() {
		return listScheme;
	}

	/**
	 * @return the schemeService
	 */
	public ISchemeService getSchemeService() {
		return schemeService;
	}

	/**
	 * @param schemeService the schemeService to set
	 */
	public void setSchemeService(ISchemeService schemeService) {
		this.schemeService = schemeService;
	}

	/**
	 * @return the schemeName
	 */
	public String getSchemeName() {
		return schemeName;
	}

	/**
	 * @param schemeName the schemeName to set
	 */
	public void setSchemeName(String schemeName) {
		this.schemeName = schemeName;
	}

	/**
	 * @return the selectedScheme
	 */
	public SchemeDVO getSelectedScheme() {
		return selectedScheme;
	}

	/**
	 * @param selectedScheme the selectedScheme to set
	 */
	public void setSelectedScheme(SchemeDVO selectedScheme) {
		this.selectedScheme = selectedScheme;
	}


}